package com.woniu113.junk.commons.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Configuration
//开启swagger
@EnableSwagger2
public class SwaggerConfig {
    @Resource
    private SwaggerProperties sp;
    //配置主要就是设置Docket对象的一些参数
    @Bean
    public Docket createDocket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        List<Parameter> pars = new ArrayList<>();//设置全局参数
        //创建一个可以携带header的全局参数(token)
        Parameter par = new ParameterBuilder().description("token").name("junk-token")
                        .parameterType("header").modelRef(new ModelRef("string")).build();
        pars.add(par);
        //创建文档的基本信息
        ApiInfo apiInfo = new ApiInfoBuilder()
                .contact(new Contact(sp.getContact().getName(),sp.getContact().getHost(),sp.getContact().getEmail()))
                .description(sp.getDesc())
                .title(sp.getTitle())
                .version(sp.getVersion())
                .build();
        //设置扫描的路径
        docket.apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage(sp.getBasePackages()))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
        return docket;
    }
}
